<?php
/*
 ATOM10.INC.PHP by martin $ 2009/03/08 02:32:00
*/

function createATOM10($time){
 global $mb, $DIVISION;
 $notutf8 = !preg_match('/utf-8/i', ENCODE) ? 1 : 0;
 $blog_name = BLOG_NAME;
 $copyright = COPY_RIGHT;
 $c_title = $blog_name;
 $root_path = ROOT_PATH; $_self = 'index.php';
 $subtitle = $blog_name.': Recent Entries';
 $gid = 'tag:'.$_SERVER['HTTP_HOST'].','.date('Y', $time).':uid.'.$time;
 $apres = MODE_STATIC ? '/' : '';
 $mod_date = date('Y-m-d',$time).'T'.date('H:i:s', $time).'Z';//YYYY-MM-DDTHH:MM:SSZ
 $generator = 'ppBlog ver.'.PPBLOG_VERSION;
 $version = PPBLOG_VERSION;
 $author = ONAME;
 $entry = '';
 $LINES = array();
 $LOGS = my_glob(EXT, 'log/');
 $length = count($LOGS);
 if($length > 0){
  for($j=0;$j<$length;$j++){
   $LINES = array_merge($LINES, my_file($LOGS[$j]));
   if(count($LINES) > RECENT_ENTRIES){
    $LINES = array_slice($LINES, 0, RECENT_ENTRIES);
    break;
   }
  }
 }
 
 foreach ($LINES as $line){
  list($uid,$category,$title,$body,$writer,) = explode("|", $line);
  $issued = date('Y-m-d',$uid).'T'.date('H:i:s', $uid).'+09:00';
  $tagid = 'tag:'.$_SERVER['HTTP_HOST'].','.date('Y', $uid).':uid.'.$uid;
  if(strpos($title, ',')){
   list($title, $pm_title) = explode(',', $title);
   $pm_link = date('Ym-', $uid).$pm_title.EXT_STATIC;
  } else $pm_link = MODE_STATIC ? $uid.EXT_STATIC : $_self.'?UID='.$uid;
  $title = htmlspecialchars(strip_tags($title));
  if(strstr($category, ',')){
   list($cat1, $cat2) = explode(',', $category);
   $cat = trans_cat($cat1).$apres; $sub = trans_cat($cat1).'/'.trans_cat($cat2).$apres;
   $category_tag = '
 <category term="'.$cat1.'" label="'.$cat1.'" scheme="'.$root_path.(MODE_STATIC ? 'category/' : 'index.php?mode=category&amp;aim=').$cat.'" xml:lang="ja" />
 <category term="'.$cat1.'/'.$cat2.'" label="'.$cat1.'/'.$cat2.'" scheme="'.$root_path.(MODE_STATIC ? 'category/' : 'index.php?mode=category&amp;aim=').$sub.'" xml:lang="ja" />';
  } else {
   $cat = trans_cat($category).$apres;
   $category_tag = '
 <category term="'.$category.'" label="'.$category.'" scheme="'.$root_path.(MODE_STATIC ? 'category/' : 'index.php?mode=category&amp;aim=').$cat.'" xml:lang="ja" />';
  }
  $body = preg_replace('{\[(?:style|script|escape)\](.+?)\[/(?:style|script|escape)\]}', '', $body);
  $link = $root_path.$pm_link;
  $_body = htmlspecialchars(strip_tags($body));
  list($body, $_body) = str_replace(array('&amp;', '&'), '$', array($body, $_body));
  $_body = preg_replace('/\[file:([^\/]+)\/\]|`/', '', $_body);
  $_body = preg_replace('{\[g\]([^\/]+?)\[/g\]}', "\\1", $_body);
  $summary = my_substr(str_replace('$', '&amp;', $_body), 0, 40);
  $summary = htmlspecialchars($summary);
  $body = my_parser($body);
  $body = preg_replace('{\[file:(.+?):(.+?)/\]}',
  '<a href="'.$root_path.'index.php?mode=download&amp;aim=\\1" title="ダウンロード:\\2 KB">[ファイル\\1]</a>', $body);
  $body = str_replace(array('<>', ' < ',' > ', '&mdash;', '$'), array('&gt;&lt;', ' &gt; ', ' &lt; ', '&#x8212;', '&amp;'), $body);
  $entry .= <<<__ENTRY

 <entry>
 <title type="text">$title</title>
 <link rel="alternate" type="text/html" href="$link" />
 <author>
   <name>$writer</name>
 </author>
 <id>$link</id>$category_tag
 <summary type="text" xml:lang="ja">$summary</summary>
 <content type="xhtml" xml:lang="ja">
  <div xmlns="http://www.w3.org/1999/xhtml">
   $body
  </div> 
 </content>
 <updated>$issued</updated>
 <published>$issued</published>
 </entry>

__ENTRY;
 }
 $atom10=<<<__ATOM10
<?xml version="1.0" encoding="utf-8"?>
 <feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ja">
 <title type="text">$blog_name</title>
 <subtitle type="text">$subtitle</subtitle>
 <link rel="alternate" type="text/html" href="$root_path" />
 <link rel="self" type="application/atom+xml" href="{$root_path}feeds/atom1.0.xml" />
 <id>$root_path</id>
 <author>
  <name>$author</name>
 </author>
 <updated>$mod_date</updated>
 <generator uri="http://p2b.jp/" version="$version">$generator</generator> 
 <rights>$copyright</rights> 
$entry
</feed>
__ATOM10;
 
 if(preg_match_all('{<img src="([^"]+?)" alt="([^"]*?)"(?:.*?)/>}i', $atom10, $mt)){ // icluding image-tags
  $imgs = array();
  $length = count($mt[0]);
  for($i=0; $i < $length; $i++){
   list(,$w,$h,$tw,$th,$alt,,,,$ttl,) = explode(':', $mt[2][$i]);
   $ttl = ($ttl != '') ? ' title="'.$ttl.'"' : '';
   if($tw * $th > 0){
    $imgs[$i] = '<img src="'.$root_path.str_replace('PIX/', 'PIX/s2/', $mt[1][$i]).'" alt="'.$alt.'" width="'.$tw.'" height="'.$th.'"'.$ttl.' />';
   } else {
    $imgs[$i] = '<img src="'.$root_path.$mt[1][$i].'" alt="'.$alt.'" width="'.$w.'" height="'.$h.'"'.$ttl.' />';
   }
  }
  $atom10 = str_replace($mt[0], $imgs, $atom10);
 }
 rewrite('feeds/atom1.0.xml', $atom10); unset($LOGS, $LINES);
}

?>